package com.example.controller;

import com.example.model.WxbCustomer;
import com.example.model.vo.Result;
import com.example.impl.WxbCustomerServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/sell")
public class WxbCustomerController {

    @Autowired
    private WxbCustomerServiceImpl wxbCustomerService;

    @RequestMapping("/list")
    public String findAll(Model model,String loginName){

       List<WxbCustomer> wxbCustomers= wxbCustomerService.findAll(loginName);
       model.addAttribute("wxbCustomers",wxbCustomers);
        return "seller-list";
    }


    @RequestMapping("/add")
    public String addCustomer(Model model,String customerId){

      WxbCustomer wxbCustomers =wxbCustomerService.findById(customerId);
      model.addAttribute("wxbCustomers",wxbCustomers);
        return "sell-add";
    }
    @RequestMapping("/save")
    @ResponseBody
    public Result save(@RequestBody WxbCustomer WxbCustomer){
        wxbCustomerService.save(WxbCustomer);
        return new Result(true,"操作成功");
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Result delete(@RequestBody HashMap<String,Object> map){

        List<String> customerId = (List<String>) map.get("customerId");
        try {
            wxbCustomerService.delete(customerId);
            return new Result(true, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "参数不足,操作失败");
        }

    }
}
